//
//  RONetworkMngTool.swift
//  neufood
//
//  Created by huangminmin on 2017/10/10.
//  Copyright © 2017年 huangminmin. All rights reserved.
//

import UIKit
//全局变量
var userId : NSString!
let single = RONetworkMngTool()
class RONetworkMngTool: NSObject {
    
    class func sharedNetWorkMngTool()-> RONetworkMngTool {
        return single
    }
    
    func RONetwork_Logon(_ parameters: NSDictionary, view: UIView, block: @escaping (_ flag: NSString) -> Void)  {
        //      MBProgressHUD.showAdded(to: view, animated: true)
        let afManager = AFHTTPRequestOperationManager()
        afManager.get("http://60.205.189.39/userLogin.do",parameters: parameters, success: {(Operation:AFHTTPRequestOperation?, responseObjct:Any?) in
            MBProgressHUD.hide(for: view, animated: true)
            //                    print(responseObjct)
            //                    let userid = (responseObjct as! NSDictionary).object(forKey: "userid") as! NSString
            //                    if userid != "0"{
            //                    print("登录成果")
            //                    }else{
            //                        print("登录失败")
            //                    }
            let logoReturnModel = ROLogonReturnModel.logonReturnModelWithDict(responseObjct as! NSDictionary)
            //当你成功登陆后保存用户名和密码
            if logoReturnModel.userid != "0" {
                userId = logoReturnModel.userid
                //print("登录成功")
                block("1")
            }else {
                MBProgressHUD.showDelayHUDToView(view, message:logoReturnModel.msg as String)
                //print("登录失败")
                block("0")
            }
            
        }, failure:{ (Operation: AFHTTPRequestOperation?, error:Error?) in
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络故障，请重试")
            block("0")
        })
        
    }
    func RegistNetwork_Logon(_ parameters: NSDictionary, view: UIView, block: @escaping (_ flag: NSString) -> Void)  {
        //      MBProgressHUD.showAdded(to: view, animated: true)
        let afManager = AFHTTPRequestOperationManager()
        afManager.get("http://60.205.189.39/userRegister.do",parameters: parameters, success: {(Operation:AFHTTPRequestOperation?, responseObjct:Any?) in
            MBProgressHUD.hide(for: view, animated: true)
            let RegReturnModel = RegistReturnModel.RegReturnModelWithDict(responseObjct as! NSDictionary)
            if RegReturnModel.success != "0" {
                //print("注册成功")
                block("1")
            }else {
                MBProgressHUD.showDelayHUDToView(view, message:RegReturnModel.msg as String)
                //print("注册失败")
                block("0")
            }
            
        }, failure:{ (Operation: AFHTTPRequestOperation?, error:Error?) in
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络故障，请重试")
            block("0")
        })
        
    }
    //获取店铺信息
    func RONetwork_GetShopList(_ view: UIView, block: @escaping (_ shopList: NSArray) -> Void)  {
        // MBProgressHUD.showAdded(to: view, animated: true)
        let afManager = AFHTTPRequestOperationManager()
        afManager.get("http://60.205.189.39/getAllShops.do",parameters: nil, success: {(Operation:AFHTTPRequestOperation?, responseObjct:Any?) in
            //    MBProgressHUD.hide(for: view, animated: true)
            //  print(responseObjct)
            //  MBProgressHUD.showAdded(to: view, animated: true)
            let shopArray = responseObjct as! NSArray
            let shopMutableArray: NSMutableArray = NSMutableArray()
            for ashopDict in shopArray{
                let shopDict = ashopDict as! NSDictionary
                let shop: ROShopModel = ROShopModel.shopModelWithDict(shopDict)
                shopMutableArray.add(shop)
            }
            block(shopMutableArray)
            
        }, failure:{ (Operation: AFHTTPRequestOperation?, error:Error?) in
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络故障，请重试")
        })
        
    }
    //获取店铺下的菜单列表
    func RONetwork_GetFoodListParameters(_ parameters: NSDictionary,  view: UIView, block: @escaping (_ courseList: NSArray) -> Void) -> Void {
        //在开始网络加载前任务指示器开始转
        MBProgressHUD.showAdded(to: view, animated: true)
        let manager = AFHTTPRequestOperationManager()
        //print(parameters)
        manager.post("http://60.205.189.39/getFoodByShop.do", parameters: parameters, success: { (operation:AFHTTPRequestOperation?, responseObject: Any?) -> Void in
            MBProgressHUD.hide(for: view, animated: true)
            //将返回的字典转换为模型对象
            //            print("菜单列表")
            //            print(responseObject)
            //            print("菜单列表")
            let foodArray = responseObject as! NSArray
            let foodMutableArray: NSMutableArray = NSMutableArray()
            
            for afoodDict in foodArray {
                let foodDict = afoodDict as! NSDictionary
                let food: ROFoodModel = ROFoodModel.foodModelWithDict(foodDict)
                foodMutableArray.add(food)
            }
            block(foodMutableArray);
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败！")
        })
    }
    //判断是否收藏
    func RONetwork_isCollectedWithParameters(_ parameters: NSDictionary,  view: UIView, block: @escaping (_ flag: NSString) -> Void) -> Void {
        
        let manager = AFHTTPRequestOperationManager()
        //print(parameters)
        manager.post("http://60.205.189.39/isCollected.do", parameters: parameters, success: { (operation:AFHTTPRequestOperation?, responseObject: Any?) -> Void in
            
            //将返回的字典转换为模型对象
            //                        print("是否收藏")
            //                        print(responseObject)
            //                        print("是否收藏")
            let res = responseObject as! NSDictionary
            let collected = res.object(forKey: "collected") as! NSString
            if collected == "1"{
                block("1")
            }else {
                block("0")
            }
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因判断收藏失败！")
        })
    }
    //收藏／取消收藏店铺
    func RONetwork_collectShopWithParameters(_ parameters: NSDictionary,  view: UIView, block: @escaping (_ flag: NSString) -> Void) -> Void {
        
        let manager = AFHTTPRequestOperationManager()
        //print(parameters)
        manager.post("http://60.205.189.39/userCollectShop.do", parameters: parameters, success: { (operation:AFHTTPRequestOperation?, responseObject: Any?) -> Void in
            //将返回的字典转换为模型对象
            //                        print("收藏店铺")
            //                        print(responseObject)
            //                        print("收藏店铺")
            let res = responseObject as! NSDictionary
            let collected = res.object(forKey: "success") as! NSString
            if collected == "1"{
                block("1")
            }else {
                block("0")
            }
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因收藏店铺失败！")
        })
    }
    //收藏／取消收藏菜单
    func RONetwork_collectFoodWithParameters(_ parameters: NSDictionary,view: UIView, block: @escaping (_ flag: NSString) -> Void) -> Void {
        
        let manager = AFHTTPRequestOperationManager()
        print(parameters)
        manager.post("http://60.205.189.39/userCollectFood.do", parameters: parameters, success: { (operation:AFHTTPRequestOperation?, responseObject: Any?) -> Void in
            //将返回的字典转换为模型对象
            //                        print("收藏菜谱")
            //                        print(responseObject)
            //                        print("收藏菜谱")
            let res = responseObject as! NSDictionary
            let collected = res.object(forKey: "success") as! NSString
            if collected == "1"{
                block("1")
            }else {
                block("0")
            }
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因收藏菜谱失败！")
            //print("网络原因收藏菜谱失败！")
        })
    }
    //根据foodid获取店铺所有信息
    func RONetwork_getShopInformationWithParameters(_ parameters: NSDictionary,view: UIView, block: @escaping (_ flag: NSString) -> Void) -> Void{
        
        let manager = AFHTTPRequestOperationManager()
        //print(parameters)
        manager.post("http://60.205.189.39/getShopByFood.do", parameters: parameters, success: { (operation:AFHTTPRequestOperation?, responseObject: Any?) -> Void in
            //将返回的字典转换为模型对象
            //            print("dianpu")
            //            print(responseObject)
            //            print("dianpu")
            let res = responseObject as! NSDictionary
            let resp = res.object(forKey: "phonenum") as! NSString
            
            block(resp)
            
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.showDelayHUDToView(view, message: "获取店铺信息失败！")
        })
    }
//    func RONetwork_GetCollectionListParameters(_ parameters: NSDictionary,  view: UIView, block: @escaping (_ courseList: NSArray) -> Void) -> Void {
//        //在开始网络加载前任务指示器开始转
//        //MBProgressHUD.showAdded(to: view, animated: true)
//        let manager = AFHTTPRequestOperationManager()
//        print(parameters)
//        manager.post("http://60.205.189.39//getAllUserCollection.do", parameters: parameters, success: { (operation:AFHTTPRequestOperation?, responseObject: Any?) -> Void in
//            //MBProgressHUD.hide(for: view, animated: true)
//            //将返回的字典转换为模型对象
//            print("所有收藏信息列表")
//            
//            //print(responseObject ?? <#default value#> ?? <#default value#>)
//            print("所有收藏信息列表")
//            let collectionListArray = responseObject as! NSArray
//            let collectionListMutableArray: NSMutableArray = NSMutableArray()
//            
//            for aCollectionDict in collectionListArray {
//                let collectionDict = aCollectionDict as! NSDictionary
//                let collection: ROCollectionModel = ROCollectionModel.collectionModelWithDict(collectionDict)
//                collectionListMutableArray.add(collection)
//            }
//            block(collectionListMutableArray);
//        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
//            //失败结束转
//            MBProgressHUD.hide(for: view, animated: true)
//            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败！")
//        })
//}

}
